/**
 * 
 * 
 * 
 * jquery extend函数的源码分析
 * 
 */
MyQuery = {};
MyQuery.extend = function() {
	var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {},
		i = 1,
		length = arguments.length,
		deep = false;

	// 传递进来的第一个参数赋给target 如果target是布尔类型的，说明该参数是深拷贝参数
	if (typeof target === "boolean") {
		deep = target;
		// 第一个不是目标对象 则第二个是目标对象，如果第二个也为空的话创建一个空对象
		target = arguments[1] || {};
		// skip the boolean and the target
		i = 2;
	}

	// Handle case when target is a string or something (possible in deep copy)
	// 当目标对象是字符串或者一些特殊情况 给target对象重新赋成一个控对象
	if (typeof target !== "object" && !jQuery.isFunction(target)) {
		target = {};
	}

	// 如果只有一个参数 继承的目标对象真为jquery本身
	if (length === i) {
		target = this;
		--i;
	}
	//这里从第二个参数开始迭代
	for (; i < length; i++) {
		// 只处理不为空的参数Only deal with non-null/undefined values
		if ((options = arguments[i]) != null) {
			// Extend the base object
			for (name in options) {
				src = target[name];
				copy = options[name];
				log(copy);
				//防止循环引用进入到死循环 Prevent never-ending loop
				if (target === copy) {
					continue;
				}

				// Recurse if we're merging plain objects or arrays
				if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)))) {
					if (copyIsArray) {
						copyIsArray = false;
						clone = src && jQuery.isArray(src) ? src : [];

					} else {
						clone = src && jQuery.isPlainObject(src) ? src : {};
					}

					// Never move original objects, clone them
					target[name] = jQuery.extend(deep, clone, copy);

					// Don't bring in undefined values
				} else if (copy !== undefined) {
					target[name] = copy;
				}
			}
		}
	}

	// Return the modified object
	return target;
};

var object1 = {
	apple: 0,
	banana: {
		weight: 52,
		price: 100
	},
	cherry: 97
};
var object2 = {
	banana: {
		price: 200
	},
	durian: 100
};

MyQuery.extend(object1, object2);
log(object1);